package org.foliage.rcp.ui.viewer;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.ContentViewer;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DragSourceListener;
import org.eclipse.swt.dnd.DropTargetListener;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.foliage.rcp.ui.custom.GanttGrid;
import org.foliage.rcp.ui.custom.GanttGridItem;
import org.foliage.rcp.ui.custom.GanttGridTask;
import org.foliage.rcp.ui.events.IGanttListener;
import org.foliage.rcp.ui.preference.DefaultGanttColorManager;
import org.foliage.rcp.ui.preference.DefaultGanttPaintManager;
import org.foliage.rcp.ui.preference.DefaultGanttSettings;
import org.foliage.rcp.ui.preference.IGanttColorManager;
import org.foliage.rcp.ui.preference.IGanttPaintManager;
import org.foliage.rcp.ui.preference.IGanttSettings;

public class GanttGridViewer extends ContentViewer {

	// ----------------------------------------------- 内部控件参数

	private Composite parent;

	private List<GanttGridViewerColumn> columns = new ArrayList<GanttGridViewerColumn>();

	private GanttGridViewerChart chart = null;

	private int usedStyle = 0;

	private IGanttSettings settings = null;

	private IGanttPaintManager paintManager = null;

	private IGanttColorManager colorManager = null;

	private IGanttGridViewerManager viewerManager = null;

	private GanttGrid ganttGrid;

	// ----------------------------------------------- 多态对象构造器

	public GanttGridViewer(Composite parent, int style) {
		this(parent, style, null);
	}

	public GanttGridViewer(Composite parent, int style, IGanttGridViewerManager viewerManager) {
		this(parent, style, null, null, null, viewerManager);
	}

	public GanttGridViewer(Composite parent, int style, IGanttSettings settings, IGanttPaintManager paintManager, IGanttColorManager colorManager) {
		this(parent, style, settings, paintManager, colorManager, null);
	}

	public GanttGridViewer(Composite parent, int style, IGanttSettings settings, IGanttPaintManager paintManager, IGanttColorManager colorManager, IGanttGridViewerManager viewerManager) {
		super();
		this.parent = parent;
		this.usedStyle = style;
		this.settings = settings != null ? settings : new DefaultGanttSettings();
		this.colorManager = colorManager != null ? colorManager : new DefaultGanttColorManager();
		this.paintManager = paintManager != null ? paintManager : new DefaultGanttPaintManager();
		this.viewerManager = viewerManager;
		init();
	}

	// -----------------------------------------------

	private void init() {
		ganttGrid = new GanttGrid(parent, usedStyle, settings, paintManager, colorManager);
		if (viewerManager != null) {
			viewerManager.fillGanttColumn(this);
			viewerManager.fillGanttChart(this);
			setContentProvider(viewerManager.getContentProvider());
		}
	}

	// -----------------------------------------------

	public void setTransfer(Transfer[] transfer) {
		ganttGrid.setTransfer(transfer);
	}

	public void addDragListener(DragSourceListener listener) {
		ganttGrid.addDragListener(listener);
	}

	public void addDropListener(DropTargetListener listener) {
		ganttGrid.addDropListener(listener);
	}

	public void addGanttListener(IGanttListener listener) {
		ganttGrid.addGanttListener(listener);
	}

	public void removeGanttListener(IGanttListener listener) {
		ganttGrid.removeGanttListener(listener);
	}

	// -----------------------------------------------

	@Override
	protected void inputChanged(Object input, Object oldInput) {
		ganttGrid.removeAll();
		IStructuredContentProvider contentProvider = (IStructuredContentProvider) getContentProvider();
		Object[] elements = contentProvider.getElements(input);
		for (Object element : elements) {
			GanttGridItem item = new GanttGridItem(ganttGrid, SWT.NONE);
			for (int i = 0; i < columns.size(); i++) {
				GanttGridViewerColumn column = columns.get(i);
				ILabelProvider labelProvider = column.getLabelProvider();
				String text = labelProvider.getText(element);
				item.setGridText(i, text);
				if (i == 0) {
					item.setGanttText(text);
				}
				item.setData(element);
			}
			IGanttGridLabelProvider ganttGridLabelProvider = chart.getLabelProvider();
			item.setData(element);
			Object[] details = ganttGridLabelProvider.getDetailElements(element);
			for (Object detail : details) {
				GanttGridTask task = new GanttGridTask(item);
				task.setText(ganttGridLabelProvider.getText(detail));
				task.setLowerCalendar(ganttGridLabelProvider.getLowerCalender(detail));
				task.setUpperCalendar(ganttGridLabelProvider.getUpperCalender(detail));
				task.setData(detail);
			}
		}
	}

	// -----------------------------------------------

	void newColumn(GanttGridViewerColumn ganttGridViewerColumn) {
		columns.add(ganttGridViewerColumn);
	}

	void newChart(GanttGridViewerChart ganttGridViewerChart) {
		chart = ganttGridViewerChart;
	}

	// -----------------------------------------------

	@Override
	public void refresh() {
		// TODO NOTHING
	}

	@Override
	public ISelection getSelection() {
		return null;
	}

	@Override
	public void setSelection(ISelection selection, boolean reveal) {
		// TODO NOTHING
	}

	// ----------------------------------------------- 

	@Override
	public Control getControl() {
		return ganttGrid;
	}

	public GanttGrid getGanttGrid() {
		return ganttGrid;
	}

	public GanttGridViewerColumn getViewerColumn(int index) {
		if (index < 0 || index >= columns.size()) {
			SWT.error(SWT.ERROR_INVALID_ARGUMENT);
		}
		return columns.get(index);
	}

}
